import { defineStore } from 'pinia'
import { ref } from 'vue'

const initState = { userInfo: {}, token: '', temToken: '' }

export const useUserStore = defineStore(
  'userStore',
  () => {
    const userState = ref<IUserInfo>({ ...initState })

    const setUserState = (val: IUserInfo) => {
      userState.value = val
    }

    const setTemToken = (val: string) => {
      userState.value.temToken = val
    }

    const getTemToken = () => {
      return userState.value.temToken
    }

    const setToken = (val: string) => {
      userState.value.token = val
    }

    const clearUserState = () => {
      userState.value = { ...initState }
    }
    // 一般没有reset需求，不需要的可以删除
    const reset = () => {
      userState.value = { ...initState }
    }
    const isLogined = computed(() => !!userState.value.token)

    const userInfo = computed(() => userState.value.userInfo)

    return {
      userState,
      setUserState,
      setTemToken,
      clearUserState,
      isLogined,
      reset,
      userInfo,
      getTemToken,
      setToken,
    }
  },
  {
    persist: true,
  },
)
